﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ScrumProject.Models;
using ScrumProject.ViewModels;

namespace ScrumProject.Controllers
{ 
    public class StoryController : Controller
    {
        private SMRPO6Context db = new SMRPO6Context();
        StoryRepository storyR = new StoryRepository();
        TaskRepository taskR = new TaskRepository();

        //
        // GET: /Story/

        public ViewResult Index(int projectId)
        {
            var stories = db.Stories.Include(s => s.Project.Id);
            ViewBag.projectId = projectId;

            //int temp = 0;
            
            //ViewBag.isHideButton = false;
       
            //List<Story> allStories = storyR.GetAllStoriesByProject(projectId).ToList<Story>();
            //foreach (Story s in allStories) {

            //    List<Task> allTasks = taskR.GetTasksByStory(s.Id).ToList<Task>();

            //    if (allTasks.Count > 0)
            //    {
            //        foreach (Task t in allTasks)
            //        {
            //            if (t.WorkRemaining.Equals(0))
            //            {
            //                temp++;
            //            }
            //        }
            //        if (temp == 0)
            //        {
            //            ViewBag.isHideButton = true;
            //        }
            //    }

            //    temp = 0;
                
            //}
               
            return View(storyR.GetAllStoriesByProject(projectId));
        }

        //
        // GET: /Story/Details/5

        public ActionResult Details(int id)
        {
            Story story = db.Stories.Find(id);
            ViewBag.projectId = story.ProjectId;

            int temp = 0;
            ViewBag.isHideButtonA = false;
            ViewBag.isHideButtonR = true;
            
            List<Task> allTasks = taskR.GetTasksByStory(story.Id).ToList<Task>();

            if (story.Status.Trim() == "active")
            {
                if (allTasks.Count > 0)
                {
                    foreach (Task t in allTasks)
                    {
                        if (!t.WorkRemaining.Equals(0.0))
                        {
                            temp++;
                        }
                    }
                    if (temp == 0)
                    {
                        ViewBag.isHideButtonA = true;
                    }
                }
            }

            //finished    
            if (story.Status.Trim() == "finished")
            {
                ViewBag.isHideButtonR = false;
            }
            return View(story);
        }

        [HttpPost]
        public ActionResult Details(Story story, string submitButton)
        {
            Story s = storyR.GetStory(story.Id);
            if (!ModelState.IsValid)
            {
                if (submitButton == "Accept")
                {
                    story.Status = "finished";
                    if (story.ProjectId == null)
                        story.ProjectId = s.ProjectId;

                    storyR.UpdateStoryStatus(story);
                    return RedirectToAction("Backlog", "MyProjects");
                }
                else
                {
                    if (story.ProjectId == null)
                        story.ProjectId = s.ProjectId;
                    story.Status = "inactive";
                    //story.SprintId = 0;

                    storyR.UpdateStoryStatus(story);
                    return RedirectToAction("Notes", new { id = story.Id });
                }
            }

            else
            {
                ViewBag.newId = story.ProjectId;
                ViewBag.ProjectId = new SelectList(db.Projects, "Id", "Name", story.ProjectId);
                return RedirectToAction("Story", "Edit");
            }
        }

        //
        // GET: /Story/Create

        public ActionResult Create(int projectId)
        {
            ViewBag.ProjectId = new SelectList(db.Projects, "Id", "Name");
            ViewBag.newId = projectId;
            return View();
        } 

        
        // POST: /Story/Create

        [HttpPost]
        public ActionResult Create(Story story)
        {
            if (ModelState.IsValid)
            {
                
                if (storyR.GetStoryFromProjectByName(story.Name, story.ProjectId.Value) == null)
                {
                    if (story.Notes == null)
                        story.Notes = "";
                    if (story.Points == null)
                        story.Points = 0;
                    if (story.Description == null)
                        story.Description = "";
                    if (story.Tests == null)
                        story.Tests = "";

                    storyR.AddStory(story);
                    return RedirectToAction("Index", new { projectId = story.ProjectId});
                }
                else
                {
                    ModelState.AddModelError("", "Story with same name already exists.");
                }
            }

            ViewBag.newId = story.ProjectId;
            ViewBag.ProjectId = new SelectList(db.Projects, "Id", "Name", story.ProjectId);
            return View(story);
        }
        
        //
        // GET: /Story/Edit/5
 
        public ActionResult Edit(int id)
        {
            Story story = db.Stories.Find(id);
            ViewBag.ProjectId = new SelectList(db.Projects, "Id", "Name", story.ProjectId);
            ViewBag.newId = story.ProjectId;
            return View(story);
        }

        //
        // POST: /Story/Edit/5

        [HttpPost]
        public ActionResult Edit(Story story)
        {
            if (ModelState.IsValid)
            {
                Story s = storyR.GetStory(story.Id);
                if (storyR.GetStoryName(story.Id) == story.Name) 
                {
                    
                    if (story.Notes == null)
                        story.Notes = s.Notes;
                    if (story.Points == null)
                        story.Points = s.Points;
                    if (story.Description == null)
                        story.Description = s.Description;
                    if (story.Tests == null)
                        story.Tests = s.Tests;
                    if (story.ProjectId == null)
                        story.ProjectId = s.ProjectId;
                    
                    story.Status = "inactive";
                    
                    storyR.UpdateStory(story);
                    //.Entry(story).State = EntityState.Modified;
                    //db.SaveChanges();
                    return RedirectToAction("Index", new { projectId = story.ProjectId });
                }

                else if (storyR.GetStoryByNameFromProject(story.Name, int.Parse(this.ControllerContext.HttpContext.Request.Cookies["projectId"].Value)) == null)
                {       
                    if (story.Notes == null)
                        story.Notes = s.Notes;
                    if (story.Points == null)
                        story.Points = s.Points;
                    if (story.Description == null)
                        story.Description = s.Description;
                    if (story.Tests == null)
                        story.Tests = s.Tests;
                    if (story.ProjectId == null)
                        story.ProjectId = s.ProjectId;
                    
                    story.Status = "inactive";

                    storyR.UpdateStory(story);
                    //db.Entry(story).State = EntityState.Modified;
                    //db.SaveChanges();
                    return RedirectToAction("Index", new { projectId = story.ProjectId });
                }

                else
                {
                    ModelState.AddModelError("", "Story with same name already exists.");
                }
            }
            ViewBag.newId = story.ProjectId;
            ViewBag.ProjectId = new SelectList(db.Projects, "Id", "Name", story.ProjectId);
            return View(story);
        }

        //
        // GET: /Story/Notes/5
 
        public ActionResult Notes(int id)
        {
            Story story = db.Stories.Find(id);
            ViewBag.ProjectId = new SelectList(db.Projects, "Id", "Name", story.ProjectId);
            ViewBag.newId = story.ProjectId;
            return View(story);
        }

        //
        // POST: /Story/Notes/5

        [HttpPost]
        public ActionResult Notes(Story story, string submitButton)
        {
            if (!ModelState.IsValid)
            {
                if (submitButton == "Cancel")
                {
                    return RedirectToAction("Backlog", "MyProjects");
                }
                else
                {
                    Story s = storyR.GetStory(story.Id);
                    if (story.ProjectId == null)
                        story.ProjectId = s.ProjectId;
                    String newNote = story.Notes;
                    story.Notes = newNote;

                    if (story.Notes == null)
                        story.Notes = "";

                    storyR.AddNoteToStory(story);
                    return RedirectToAction("Backlog", "MyProjects");//RedirectToAction("Index", new { projectId = story.ProjectId });
                }
            }
            else
            {
                ViewBag.newId = story.ProjectId;
                ViewBag.ProjectId = new SelectList(db.Projects, "Id", "Name", story.ProjectId);
                return RedirectToAction("Backlog", "MyProjects");
            }
        }

        //
        // GET: /Story/Delete/5
 
        public ActionResult Delete(int id)
        {
            Story story = db.Stories.Find(id);
            ViewBag.projectId = story.ProjectId;
            return View(story);
        }

        //
        // POST: /Story/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Story story = db.Stories.Find(id);
            int pid = (int)story.ProjectId;
            db.Stories.Remove(story);
            db.SaveChanges();
            return RedirectToAction("Index", new { projectId = pid });
        }

        
        

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}